package com.fjzx.androidframe.base.application;

import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * 基础Application类
 * 在Application每个生命周期方法中，调用代理Application的生命周期方法
 *
 * @author xiongyongcong
 */
public class BaseApp extends Application {
    /**
     * 模块Application代理
     */
    protected static List<AppDelegate> appDelegates = new ArrayList<>();

    protected boolean isDebug() {
        return false;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        for (AppDelegate delegate : appDelegates) {
            delegate.setDebug(isDebug());
            delegate.onCreate();
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        for (AppDelegate delegate : appDelegates) {
            delegate.attachBaseContext(base, this);
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        for (AppDelegate delegate : appDelegates) {
            delegate.onTerminate();
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        for (AppDelegate delegate : appDelegates) {
            delegate.onLowMemory();
        }
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        for (AppDelegate delegate : appDelegates) {
            delegate.onTrimMemory(level);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        for (AppDelegate delegate : appDelegates) {
            delegate.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void registerComponentCallbacks(ComponentCallbacks callback) {
        super.registerComponentCallbacks(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.registerComponentCallbacks(callback);
        }
    }

    @Override
    public void unregisterComponentCallbacks(ComponentCallbacks callback) {
        super.unregisterComponentCallbacks(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.unregisterComponentCallbacks(callback);
        }
    }

    @Override
    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.registerActivityLifecycleCallbacks(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.registerActivityLifecycleCallbacks(callback);
        }
    }

    @Override
    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.unregisterActivityLifecycleCallbacks(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.unregisterActivityLifecycleCallbacks(callback);
        }
    }

    @Override
    public void registerOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
        super.registerOnProvideAssistDataListener(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.registerOnProvideAssistDataListener(callback);
        }
    }

    @Override
    public void unregisterOnProvideAssistDataListener(OnProvideAssistDataListener callback) {
        super.unregisterOnProvideAssistDataListener(callback);
        for (AppDelegate delegate : appDelegates) {
            delegate.unregisterOnProvideAssistDataListener(callback);
        }
    }
}

